//
//  SSNormalSectionHeader.swift
//  拾舍清洁
//
//  Created by sky.wei on 2023/5/30.
//

import UIKit

class SSNormalSectionHeader: UIView {

    @IBOutlet weak var whiteCardTopConstent: NSLayoutConstraint!
    @IBOutlet weak var headerBG: UIImageView!
    @IBOutlet weak var white_card: UIView!
    
    @IBOutlet weak var mainTitleLab: UIImageView!
    @IBOutlet weak var titleLab: UILabel!
    
    @IBOutlet weak var describeLab: UILabel!
    
    @IBOutlet weak var mainImg: UIImageView!
    
    @IBOutlet weak var itemImg: UIImageView!
    
    @IBOutlet weak var itemTitle: UILabel!
    
    @IBOutlet weak var tagLab: UILabel!
    
    @IBOutlet weak var tagBGView: UIView!
    
    @IBOutlet weak var priceLab: UILabel!
    
    @IBOutlet weak var tagLab2: UILabel!
    class func headerView()->SSNormalSectionHeader{
        return Bundle.main.loadNibNamed("SSNormalSectionHeader", owner: nil, options: nil)?.last as! SSNormalSectionHeader
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        white_card.layer.cornerRadius = 12
        white_card.layer.maskedCorners = [.layerMaxXMinYCorner]
        
    }
    
    var monthM: SSNormal = SSNormal() {
        didSet {
            titleLab.text = monthM.title
            describeLab.text = monthM.describe
            mainImg.setImage(with: monthM.imgUrl)
            let firstItem = monthM.listPackage.first
            guard let item = firstItem else {
                return
            }
//            itemTitle.text = item.pkgName
            tagLab.text = item.pkgType
            itemImg.setImage(with: item.pkgImg)
            guard let price = item.listSpecs.first?.specsPrice else{
                return
            }
            priceLab.text = "$\(price)"
//            normalPrice(label: priceLab, price: price)
            tagLab2.text = splitStr(datas: item.listTag)
            
            //标题处理
            let str2:NSString = item.pkgName as NSString
            let range =  str2.range(of: item.keyword)
            
            let str = stringInsert(string: item.pkgName, keyword: item.keyword, insertStr: " ")
            normalStrToTitle(textLab: itemTitle, string: str, range: range)

        }
    }
    
    @IBAction func detailAction(_ sender: Any) {
        let vc = SSNormalDetailController()
        vc.normal = monthM.listPackage.first!
        UIViewController.current().navigationController?.pushViewController(vc, animated: true)
    }
}

//字符串插入
func stringInsert(string:String,keyword:String,insertStr:String) -> String {
    let str2:NSString = string as NSString
    let range =  str2.range(of: keyword)
    var str = string
    str.insert(contentsOf: " ", at: str.index(str.startIndex, offsetBy: range.location + range.length))
    print(str)
    return str
}

///字符串分割
func splitStr(datas:[String],splitStr:String = " | ") -> String {
    var tagStr = ""
    for(index, subItem) in datas.enumerated(){
        if(index == 0){
            tagStr = subItem
        }else{
//            tagStr = "\(tagStr) \(splitStr) \(subItem)"
            tagStr = tagStr + splitStr + subItem
        }
    }
    return tagStr
}


func normalStrToTitle(textLab:UILabel,string:String,range:NSRange) {
    let attributedString = NSMutableAttributedString(string:string)
    attributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.init(hex: "#187BD0")!, range: range)
    attributedString.addAttribute(NSAttributedString.Key.font, value: UIFont.boldSystemFont(ofSize: 20), range: range)
    textLab.attributedText = attributedString
}

func normalStrToTitle2(textLab:UILabel,string:String,range:NSRange) {
    let attributedString = NSMutableAttributedString(string:string)
    attributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.init(hex: "#187BD0")!, range: range)
    attributedString.addAttribute(NSAttributedString.Key.font, value: UIFont.boldSystemFont(ofSize: 18), range: range)
    textLab.attributedText = attributedString
}

